﻿using Common;
using Common.Consts;
using Common.Helpers;
using Common.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinServiceClient.Helpers;

namespace WinServiceClient
{
    public partial class WinServiceClient : Form
    {
        public WinServiceClient()
        {
            InitializeComponent();
        }
        string serviceFilePath = $"{Application.StartupPath}\\WinServiceFileListener.exe";
        //string serviceName = "FileListenerService";

        private void WinServiceClient_Load(object sender, EventArgs e)
        {
            lblDesc.Text = "";

            AddService service = new AddService();

            cboServiceName.DataSource = service.serviceList;
            cboServiceName.ValueMember = "ServiceName";
            cboServiceName.DisplayMember = "ServiceName";

            var operService = IniHelper.ReadOrWrite(Sections.OperServiceName, "", "正在操作的服务名称");
            if (string.IsNullOrEmpty(operService))
            {
                cboServiceName.SelectedIndex = 0;
                var model = GetCboModel();
                IniHelper.WriteValue(Sections.OperServiceName, model.ServiceName);
            }
            else
            {
                cboServiceName.SelectedValue = operService;
            }

            lblDesc.Text = GetCboDesc();
        }
        //安装 
        private void btnInstall_Click(object sender, EventArgs e)
        {
            string serviceName = GetCboModel()?.ServiceName;

            if (ServiceHelper.IsServiceExisted(serviceName))
            {
                lblTips.Text = $"{serviceName}正在重装中，请稍等...";
                var task = ServiceHelper.UninstallServiceAync(serviceFilePath, serviceName);
                task.ContinueWith((t) =>
                {
                    lblTips.Text = $"{serviceName}正在安装...";
                    ServiceHelper.InstallService(serviceFilePath, serviceName);
                    lblTips.Text = $"{serviceName}安装成功";
                });
                
            }
            else
            {
                lblTips.Text = $"{serviceName}正在安装中...";
                ServiceHelper.InstallService(serviceFilePath, serviceName);
                lblTips.Text = $"{serviceName}安装成功";
            }
        }
        //卸载
        private async void btnUnInstall_Click(object sender, EventArgs e)
        {
            string serviceName = GetCboModel()?.ServiceName;

            if (ServiceHelper.IsServiceExisted(serviceName))
            {
                lblTips.Text = $"{serviceName}卸载中...";
                ServiceHelper.ServiceStop(serviceName);
                await ServiceHelper.UninstallServiceAync(serviceFilePath, serviceName);
                lblTips.Text = $"{serviceName}卸载成功";
            }
            else
            {
                lblTips.Text = $"{serviceName}有可能未安装";
            }
        }
        //启动
        private void btnStart_Click(object sender, EventArgs e)
        {
            string serviceName = GetCboModel()?.ServiceName;

            if (ServiceHelper.IsServiceExisted(serviceName))
            {
                ServiceHelper.ServiceStart(serviceName);
                lblTips.Text = $"{serviceName}启动成功";
            }
            else
            {
                lblTips.Text = $"{serviceName}有可能未安装";
            }
        }
        //停止
        private void btnStop_Click(object sender, EventArgs e)
        {
            string serviceName = GetCboModel()?.ServiceName;

            if (ServiceHelper.IsServiceExisted(serviceName))
            {
                ServiceHelper.ServiceStop(serviceName);
                lblTips.Text = $"{serviceName}停止成功";
            }
            else
            {
                lblTips.Text = $"{serviceName}有可能未安装";
            }
        }

        private string GetCboDesc()
        {
            var model = (ServiceModel)cboServiceName.SelectedItem;
            return model?.Description;
        }
        private ServiceModel GetCboModel()
        {
            ServiceModel model = (ServiceModel)cboServiceName.SelectedItem;
            return model;
        }
        private void cboServiceName_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (cboServiceName.DataSource != null && cboServiceName.SelectedItem != null)
            {
                var model = GetCboModel();
                lblDesc.Text = model.Description;

                IniHelper.WriteValue(Sections.OperServiceName, model.ServiceName);
            }
        }

        private void btnAllInstall_Click(object sender, EventArgs e)
        {
            
        }
        private void btnAllUnInstall_Click(object sender, EventArgs e)
        {

        }
        private void btnAllStart_Click(object sender, EventArgs e)
        {

        }
        private void btnAllStop_Click(object sender, EventArgs e)
        {

        }

        
    }
}
